var rewire = require("rewire")
const { elemFindItem, elemClick, elemGetHtml, elemGetItem, elemLocation, elemGetList, elemFindList, elemGetValue, elemGetText, elemInput, elemInputSet, elemWaitUtil, elemExists, createWebDriver, getWebDriver, driverGoto, driverBack, driverRefresh, driverSetCookies, driverScollPage, driverSendString, driverWaitTime } = rewire('./common/webdriver.cjs')
const { readFile } = rewire('./common/file.cjs')
const { getRandomGreeting } = rewire('./state.cjs')
const utils = rewire('./common/utils.cjs')
const {filterAsync} = rewire('./common/combinator.cjs')


let client = null
(async function() {
    client = await initTwitterClient()
})()
greetingUser(client, 'iamsuperbianca')

async function initTwitterClient() {
    const client = await createWebDriver('twitter')
    await driverGoto(client, 'https://twitter.com/home')
    const cookies = readFile('twitter.cookies')
    await driverSetCookies(client, cookies)
    await driverRefresh(client)
    return client
}

async function twitterAutoGreeting(client) {
    const doFn = composeEachAsync(
	async function() {
	    return await getTargetUids()
	},
	async function(uid) {
	    await greetingUser(client, uid)
	}
    )
    await doFn()
}

async function greetingUser(client, uid) {
    await driverGoto(client, 'https://twitter.com/' + uid)
    
    await elemWaitUtil(client, 'section[aria-labelledby="accessible-list-1"]', async (el) => {
	return await el.isExisting()
    })
    await (repeatTimesFn(async () => {
	await driverScollPage(client)
	await driverWaitTime(client, 500)
    }, 5))()
    const elList = await findRecentItem(client)
    const el = elList[0]
    await driverScollElem(el)
    await elemClick(el)
    
    const inputBox = await elemGetItem(client, ".DraftEditor-root")
    await elemClick(inputBox)
    await driverSendString(client, getRandomGreeting())
    
    const commentBtn = await elemGetItem(client, "[data-testid='tweetButtonInline']")
    await elemClick(commentBtn)
    await driverWaitTime(client, 500)
    await driverBack(client)
}

async function findRecentItem(driver) {
    const name = await getUserName(driver)
    const items = await elemGetList(driver, 'article')
    const filtedItems = await filterAsync(items, async (item) => {
	const authorName = await getAuthorName(item)
	return (authorName == name) && !(await isPinned(item))
    })
    return filtedItems
}

async function getUserName(client) {
    const el = await elemGetItem(client, '.css-4rbku5.css-901oao.css-bfa6kz.r-18jsvk2.r-37j5jr.r-adyw6z.r-b88u0q.r-135wba7.r-bcqeeo.r-qvutc0[role="heading"]')
    const text = await elemGetText(el)
    return text
}

async function isPinned(item) {
    const el = await item.$('.r-14j79pv.r-4qtqp9.r-yyyyoo.r-10ptun7.r-dnmrzs.r-bnwqim.r-1plcrui.r-lrvibr.r-1janqcz')
    const rs = await el.isExisting()
    return rs
}

async function getAuthorName(item) {
    return await item.$('.css-901oao.r-1awozwy.r-18jsvk2.r-6koalj.r-37j5jr.r-a023e6.r-b88u0q.r-rjixqe.r-bcqeeo.r-1udh08x.r-3s2u2q.r-qvutc0').getText()
}


async function getTargetUids() {
    return ['iamsuperbianca']
}

async function driverScollElem(elem) {
    await elem.scrollIntoView()
}

function repeatTimesFn(f, times) {
    return async function(...args) {
	for (let i = 0; i < times; i++) {
	    await f(...args)
	}
    }
}
